package com.gcph.zhuoba.main.activitys;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gcph.zhuoba.R;
import com.gcph.zhuoba.main.bean.Resource;
import com.gcph.zhuoba.main.bean.ResourceData;
import com.gcph.zhuoba.main.common.CommonVedioActivity;
import com.gcph.zhuoba.main.common.Constant;
import com.gcph.zhuoba.main.view.BallJiasaiDisplayView;
import com.gcph.zhuoba.player.activitys.PlayerPracticeActivity;
import com.gcph.zhuoba.player.activitys.PlayerPreviewActivity;
import com.gcph.zhuoba.util.DownImage;
import com.gcph.zhuoba.util.L;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
/**
 * 资源详情页面
 * 
 * @author wangzhuquan
 */
public class ResourceDetailActivity extends Activity {
	private String resourceId;// 资源ID
	private String resourceDesc;// 资源描述
	
	//标识资源是否内置固定的教案
	private String flag;
	
	//private VideoView videoView;
	
	private ImageView video_preview;
	private String videoFileUrl;
	
	//资源描述控件
	private TextView descTextView;
	//我要练习
	private Button doButton;
	private Button previewButton;
	private ImageButton back;
	private ImageButton pub;
	//击打力度
	private ProgressBar resource_detail_ball_power;
	
	private TextView resource_type_text;
	
	private ImageView resourceDetailImg;
	private BallJiasaiDisplayView ballJiasaiDisplayView;
	private Resource resource; 
	private ResourceData gesourceData;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_resource_detail);
		Intent intent=getIntent();
		resourceId=intent.getStringExtra("resourceId");
		//resourceDesc=intent.getStringExtra("resourceDesc");
		flag = intent.getStringExtra("flag");
		String videoUrl=intent.getStringExtra("videoFileUrl");
		
		L.d("---------------videoUrl-"+videoUrl);
				
		descTextView = (TextView) findViewById(R.id.resource_detail_desc);
		if(null != resourceDesc)
		{
			descTextView.setText(resourceDesc);
		}
		
		//初始化按钮，并设置监听
		viewInitAndSetClickListener();
	}
	
	@Override
	protected void onResume() {
		new GetResourceTask().execute();
		super.onResume();
	}
	
	/**
	 * 初始化按钮，并设置监听
	 */
	private void viewInitAndSetClickListener()
	{
		doButton = (Button) findViewById(R.id.resource_detail_do);
		previewButton = (Button) findViewById(R.id.resource_detail_preview);
		resource_type_text = (TextView) findViewById(R.id.resource_type_text);
		//默认为不可用的，等教案信息加载完以后变为可用
		doButton.setEnabled(false);
		
		if(null != flag && flag.equals("neizhi"))
		{
			previewButton.setVisibility(View.VISIBLE);
		}else
		{
			doButton.setText(getString(R.string.btn_do_tiaozhan));
		}
			
		
		doButton.setTag(resourceId);
		back = (ImageButton) findViewById(R.id.btn_back);
		pub = (ImageButton) findViewById(R.id.btn_share);
		
		resourceDetailImg = (ImageView) findViewById(R.id.resource_detail_img);
		ballJiasaiDisplayView = (BallJiasaiDisplayView) findViewById(R.id.ball_jiasai_displayView); 
		
		//击打力度
		resource_detail_ball_power = (ProgressBar) findViewById(R.id.resource_detail_ball_power); 
		
		ballJiasaiDisplayView.setZOrderOnTop(true);//设置画布  背景透明
		ballJiasaiDisplayView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
		
		// 设置返回按钮事件
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ResourceDetailActivity.this.finish();
			}
		});
		// 设置发布分享按钮事件
		pub.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
		
		//单击我要习按钮,去练习界面
		doButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ResourceDetailActivity.this,PlayerPracticeActivity.class);
				//把当前资源传过去
				Bundle bundel = new Bundle();
				bundel.putSerializable("resource", resource);
				intent.putExtras(bundel);
				startActivity(intent);
			}
		});
		
		
		//单击预览，进行预览操作
		previewButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(null == resource || null== resource.getResourceSmallType())
				{
					return;
				}
				
				Intent intent = new Intent(ResourceDetailActivity.this, PlayerPreviewActivity.class);
				intent.putExtra("method", "804");
				//发送教案类型 zouwei、kqiu、 fangshou、jieqiu 
				if(Constant.RESOURCE_SMALL_TYPE_ZOUWEI.equals(resource.getResourceSmallType()))
				{
					if(resource.getId().intValue()==4)
					{
						intent.putExtra("data", "fangshou");
					}else
					{
						intent.putExtra("data", "zouwei");
					}
					
				}else if(Constant.RESOURCE_SMALL_TYPE_KQIU.equals(resource.getResourceSmallType()))
				{
					intent.putExtra("data", "kqiu");
				}else if(Constant.RESOURCE_SMALL_TYPE_JIEQIU.equals(resource.getResourceSmallType()))
				{
					intent.putExtra("data", "jieqiu");
				}else if(Constant.RESOURCE_SMALL_TYPE_FANGSHOU.equals(resource.getResourceSmallType()))
				{
					intent.putExtra("data", "fangshou");
				}
				startActivity(intent);
			}
		});
		
		video_preview = (ImageView) this.findViewById(R.id.video_preview);
	}
	
	/**
	 * 异步读取教案信息
	 * @author zhanghejie
	 */
	private class GetResourceTask extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			HttpUtils http = new HttpUtils();
			http.configCurrentHttpCacheExpiry(0l);//不缓存请求数据
			
			RequestParams httpParams=new RequestParams();
			httpParams.addBodyParameter("id", resourceId);
			http.send(HttpRequest.HttpMethod.POST, getResources().getString(R.string.api_resource_get),httpParams,
					new RequestCallBack<String>() {
						@Override
						public void onSuccess(ResponseInfo<String> responseInfo) {
							
							String response = responseInfo.result;
							L.d("GetResourceTask response=="+response);
							resource = new Gson().fromJson(response, Resource.class);
							if(null != resource)
							{
								// 如果存在网络图片，需要网络加载
								DownImage downImage = new DownImage(resourceDetailImg);
								downImage.execute(getResources().getString(R.string.server_root_url) + resource.getImgFileUrl());
								
								gesourceData =  new Gson().fromJson(resource.getResourceData(), ResourceData.class);
								resourceDesc = resource.getResourceDesc();
								descTextView.setText(resourceDesc);
								
								L.d("resource id=="+resource.getId());
								L.d("resource imgFileUrl=="+resource.getImgFileUrl());
								L.d("gesourceData=="+resource.getResourceData());
								L.d("ballJiasaiDisplayView=="+ballJiasaiDisplayView);
								if(null != gesourceData)
								{
									if(null != ballJiasaiDisplayView)
									{
										ballJiasaiDisplayView.setJiasai(gesourceData.getJiasaiX(), gesourceData.getJiasaiY());
									}
									//设置击打力度
									int power = gesourceData.getBallPower();
									resource_detail_ball_power.setProgress(power);
								}
								resource_type_text.setText(Constant.getResourceText(resource.getResourceType(), resource.getResourceSmallType()));
								videoFileUrl=resource.getVideoFileUrl();
								
								if(null != videoFileUrl && !"".equals(videoFileUrl) && !videoFileUrl.contains("http"))
								{
									video_preview.setOnClickListener(new OnClickListener() {
										@Override
										public void onClick(View v) {
											Intent intent = new Intent(ResourceDetailActivity.this,CommonVedioActivity.class);
											intent.putExtra("videoFileUrl", videoFileUrl);
											startActivity(intent);
										}
									});

								}
								doButton.setEnabled(true);
							}
						}

						@Override
						public void onFailure(HttpException error, String msg) {
						}
					});
			
			return null;
		}
	}
}